package Messages;

import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import ocsf.server.ConnectionToClient;
import Server.SQLConnection;

public class MessageLogin {
	private ConnectionToClient client ;
	private ArrayList<Object> str ; 
	
	public MessageLogin(ArrayList<Object> msg, ConnectionToClient client){
		this.client= client ;
		this.str= msg; 		
	}
	

public void Autorization() throws SQLException, IOException{
	
        
          SQLConnection conn= new SQLConnection();
          java.sql.PreparedStatement ps = conn.getConnection().prepareStatement("SELECT u.`FirstName`, u.`Password` ,u.`Role`,u.`Connected` FROM HTS.users u WHERE FirstName='"+str.get(1)+"' AND Password='"+str.get(2)+"';");
          ResultSet rs = ps.executeQuery();
         
          if (!rs.next())
			{		
        	    ArrayList<Object> Run =new ArrayList<Object>();
    			Run.add("NotInTheSystem");
				client.sendToClient(Run); // if the user was not found 
				
			}//end of if 
		
		else{
      	  if(rs.getString(3).equals("0"))
				{
      			
      		  
      		  	if(rs.getString(4).equals("1")){
      		  	ArrayList<Object> Run =new ArrayList<Object>();
      			Run.add("MultipleConnection");
              	  client.sendToClient(Run); // need to compleate this messag
              	  }
      		  	else{
      		  		java.sql.PreparedStatement psl = conn.getConnection().prepareStatement("Update HTS.users SET Connected=? WHERE FirstName=?");	
	        		  	psl.setInt(1, 1);
	        		  	psl.setString(2, (String) str.get(1));
	        		  	psl.executeUpdate();
	        			
	        			
	        			SQLConnection sc = new SQLConnection();
	        			PreparedStatement Allps = sc.getConnection().prepareStatement("SELECT * FROM Run;");	
	        			
	        			ResultSet Allrs= Allps.executeQuery();	        			
	        			ArrayList<Object> Center =new ArrayList<Object>();
	        			Center.add("Manager");
	        			
	        			while (Allrs.next())
	        			{
	        				Center.add(Allrs.getInt(1));
	        				Center.add(Allrs.getString(2));
	        				Center.add(Allrs.getString(3));
	        				Center.add(Allrs.getInt(4));
	        				Center.add(Allrs.getString(5));

	        			}
	        			
	        			
	        			PreparedStatement CCRps = sc.getConnection().prepareStatement("SELECT SName FROM hts.project where Project='CCR';");	
	        			
	        			ResultSet CCRrs= CCRps.executeQuery();	        			
	        			
	        			Center.add("CCR");
	        			
	        			while (CCRrs.next())
	        			{			
	        				Center.add(CCRrs.getString(1));
	        			//	String[] str = CCRrs.getString(1).split("_");
	        				//Center.add(str[1]);
	        			}
	        			
	        			PreparedStatement VRSps = sc.getConnection().prepareStatement("SELECT SName FROM hts.project where Project='VRS';");	
	        			
	        			ResultSet VRSrs= VRSps.executeQuery();	        			
	        			
	        			Center.add("VRS");
	        			
	        			while (VRSrs.next())
	        			{			
	        				Center.add(VRSrs.getString(1));
	        			
	        			}
	        			
	        			PreparedStatement Otherps = sc.getConnection().prepareStatement("SELECT SName FROM hts.project where Project='Other';");	
	        			
	        			ResultSet Otherrs= Otherps.executeQuery();	        			
	        			
	        			Center.add("Other");
	        			
	        			while (Otherrs.next())
	        			{			
	        				Center.add(Otherrs.getString(1));
	        			
	        			}
	        			
	        			client.sendToClient(Center);
	        			
      		  	}
				}//end of if 
		}
}
}